/*
 * @Author: atony2099
 * @Date: 2019-02-13 13:46:08
 * @Last Modified by: atony2099
 * @Last Modified time: 2019-02-15 10:54:32
 */

'use strict';

const Service = require('egg').Service;

class SignService extends Service {
  async getUserByQuery(query) {
    const { User } = this.ctx.model;
    return User.findOne(query);
  }

  async updateUserById(_id, updateInfo) {
    const { User } = this.ctx.model;
    return User.findOneAndUpdate(
      { _id },
      { $set: Object.assign({ updateAt: new Date() }, updateInfo) },
      {
        new: true,
      }
    );
  }

  async findUserByLoginnameAndPass(loginname, password) {
    const { User } = this.ctx.model;
    return User.findOne({ loginname, password });
  }

  async resetUserInfo(_id) {
    const { User } = this.ctx.model;
    return User.findOneAndUpdate(
      { _id },
      {
        $unset: { goldCount: '', lastSignDate: '', continusDay: '' },
        $set: { updateAt: new Date() },
      },
      { new: true }
    );
  }
}

module.exports = SignService;
